package com.wang.util;

import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;

import com.wang.common.util.ConstatFinalUtil;
import com.wang.pojo.AAdmins;
import com.wang.service.IEmailOperService;

/**
 * 多线程
 * 
 * @author wangsh_sxt
 *
 */
public class ServiceCallableUtil implements Callable<Object>
{
	/* 发送邮件的标识 */
	public static final String OPERTYPE_RESULTBATCH = "resultbatch";
	
	/* 操作类型 */
	private String operType;
	/* 存储参数的map */
	private Map<String, Object> paramsMap;

	@Override
	public Object call() throws Exception
	{
		try
		{
			String thid = UUID.randomUUID().toString().replaceAll("-", "");
			Date createTime = new Date();
			paramsMap.put("createTime", createTime);
			paramsMap.put("thid", thid);
			// 1是开始,0是停止
			paramsMap.put("status", "1");
			ConstatFinalUtil.THREAD_MAP.put(thid, paramsMap);

			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池开始-----");
			if (OPERTYPE_RESULTBATCH.equalsIgnoreCase(operType))
			{
				// 多线程发邮件
				resultbatch();
			}

			// 将线程移除掉
			ConstatFinalUtil.THREAD_MAP.remove(thid);
			ConstatFinalUtil.SYS_LOGGER.info(thid + "-----线程池结束-----");
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("启动多线程报错了;条件:{}", paramsMap + ";operType:" + this.operType, e);
		}
		return 0;
	}


	/**
	 * 批量处理邮件
	 */
	private void resultbatch() {
		IEmailOperService emailOperService = (IEmailOperService) paramsMap.get("service");
		emailOperService.verifyBatchEmailService();
	}

	public void setOperType(String operType)
	{
		this.operType = operType;
	}

	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}
}
